<template>
  <el-dialog title="新增部门" :visible="visible" width="30%" @close="closeFn">
    <el-form
      ref="deptForm"
      :model="formData"
      :rules="formRules"
      :submit="submit"
      label-width="120px"
    >
      <el-form-item prop="name" label="部门名称">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item prop="code" label="部门编码">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item prop="manager" label="部门负责人">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @focus="getEmployeeSimple">
          <el-option v-for="item in employeesList" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item prop="introduce" label="部门介绍">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeFn">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartment } from '@/api/department'

export default {
  filters: {},
  components: {},
  props: {
    visible: { type: Boolean, required: true },
    currentNode: {
      type: Object,
      required: true
    },
    originDeps: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      formRules: {
        name: [
          {
            required: true,
            message: '部门名称不能为空',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 50,
            message: '部门名称要求1-50个字符',
            trigger: 'blur'
          },
          {
            validator: (rule, value, callback) => {
              const isRepeat = this.currentNode.children?.some(
                (item) => item.name === value
              )
              isRepeat ? callback(new Error('部门重复')) : callback()
            },
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '部门编码不能为空',
            trigger: 'blur'
          },
          {
            min: 1,
            max: 50,
            message: '部门编码要求1-50个字符',
            trigger: 'blur'
          },
          {
            validator: (rule, value, callback) => {
              const isRepeat = this.originDeps.children?.some(
                (item) => item.name === value
              )
              isRepeat ? callback(new Error('部门编码重复')) : callback()
            },
            trigger: 'blur'
          }
        ],
        manager: [
          {
            required: true,
            message: '部门负责人不能为空',
            trigger: 'blur'
          }
        ],
        introduce: [
          {
            required: true,
            message: '部门介绍不能为空',
            trigger: 'blur'
          },
          {
            trigger: 'blur',
            min: 1,
            max: 300,
            message: '部门介绍要求1-50个字符'
          }
        ]
      },
      employeesList: []
    }
  },
  computed: {},
  watch: {},
  methods: {
    closeFn() {
      this.$refs.deptForm.resetFields() // 重置校验字段
      this.$emit('update:visible', false)
    },
    async getEmployeeSimple() {
      this.employeesList = await getEmployeeSimple()
    },
    submit() {
      this.$refs.deptForm.validate(async isOk => {
        if (isOk) {
          addDepartment({ ...this.formData, pid: this.currentNode.id })
          this.$emit('updateDeps') // 触发自定义事件
          this.closeFn()
        }
      })
    }
  }
}
</script>

<style scoped>
/* 行内元素不能设置tac,转化为行内块里面的元素就可以了 */
.dialog-footer {
  width: 100%;
  text-align: center;
  display: inline-block;
}
</style>
